package com.gz.logistics.app;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.gz.logistics.R;
import com.gz.logistics.fragment.HomeFragment;
import com.gz.logistics.fragment.OutingFragment;
import com.gz.logistics.fragment.PersonFragment;
import com.gz.logistics.fragment.QureyOrderFragment;
import com.gz.logistics.library.IOrder;
import com.gz.logistics.library.listener.CheckVersionListener;
import com.gz.logistics.library.pojo.CheckVersionInfo;
import com.gz.logistics.library.util.CrashHandler;
import com.gz.logistics.library.util.PreferenceModel;
import com.gz.logistics.util.ToastUtils;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {


    private static final String TAG = MainActivity.class.getSimpleName();

    private int[] mMenuDefaultRec;
    private int[] mMenuSelectedRec;
    private int mCurrentMenu;
    private long mBackKeyPressedTime;
    private FragmentAdapter mFragmentAdapter;
    private ImageView[] mIvMenu;
    private ViewPager mViewPager;
    public static Context context;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        context = MainActivity.this;
        //开启推送服务
        mMenuDefaultRec = new int[] {
                R.mipmap.ic_menu_order_default,
                R.mipmap.ic3,
                R.mipmap.ic8,
                R.mipmap.ic6
        };

        mMenuSelectedRec = new int[] {
                R.mipmap.ic_menu_order_selected,
                R.mipmap.ic4,
                R.mipmap.ic7,
                R.mipmap.ic5
        };

        mFragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(mFragmentAdapter);
        mViewPager.setOnPageChangeListener(this);
        mViewPager.setOffscreenPageLimit(3);

        mIvMenu = new ImageView[] {
                (ImageView) findViewById(R.id.iv_menu_shopping_cart),
                (ImageView) findViewById(R.id.iv_menu_participation),
                (ImageView) findViewById(R.id.iv_menu_mall),
                (ImageView) findViewById(R.id.iv_menu_personal) };

        View menu = findViewById(R.id.layout_menu_shopping_cart);
        menu.setTag(0);
        menu.setOnClickListener(this);
        menu = findViewById(R.id.layout_menu_participation);
        menu.setTag(1);
        menu.setOnClickListener(this);
        menu = findViewById(R.id.layout_menu_mall);
        menu.setTag(2);
        menu.setOnClickListener(this);
        menu = findViewById(R.id.layout_menu_personal);
        menu.setTag(3);
        menu.setOnClickListener(this);

        int position = getIntent().getIntExtra("page_index", 0);
        selectPager(position);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        CrashHandler.d(TAG, TAG + " onNewIntent");
        int position = intent.getIntExtra("page_index", 0);
        selectPager(position);
    }

    private void selectPager(int position) {
        changeMenu(position);
        mViewPager.setCurrentItem(position, false);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mBackKeyPressedTime) > 1000) {
                ToastUtils.showToast(this, "再按一次退出程序", Toast.LENGTH_SHORT);
                mBackKeyPressedTime = System.currentTimeMillis();

            } else {
                this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageSelected(int position) {
        changeMenu(position);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_menu_mall:
            case R.id.layout_menu_participation:
            case R.id.layout_menu_shopping_cart:
            case R.id.layout_menu_personal:
                if (mCurrentMenu != (Integer) v.getTag())
                    mViewPager.setCurrentItem((Integer) v.getTag(), false);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void changeMenu(int position) {
        mIvMenu[mCurrentMenu].setImageResource(mMenuDefaultRec[mCurrentMenu]);
        mIvMenu[position].setImageResource(mMenuSelectedRec[position]);
        mCurrentMenu = position;
    }


    public static class FragmentAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragments;

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
            mFragments = new ArrayList<Fragment>();
            mFragments.add(new QureyOrderFragment());// 订单查询
            mFragments.add(new OutingFragment());// 预出库单
            mFragments.add(new HomeFragment());//收衣送衣
            mFragments.add(new PersonFragment());// 我页面


        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            Object obj = super.instantiateItem(container, position);
            CrashHandler.d(TAG, "FragmentAdapter's instantiateItem");
            return obj;
        }

    }

}
